"""时间转换"""
import datetime
import random
import time

import pytz

timezone = pytz.timezone('Asia/Shanghai')


def datetime_to_isodate(dt: datetime.datetime) -> datetime.datetime:
    """convert datetime object into date object"""
    return dt.astimezone(timezone)


def strdate_to_datetime(date_str: str) -> datetime.datetime:
    """convert string date into datetime object"""
    return datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")


def current_datetime(is_timezone: bool = False) -> datetime.datetime:
    """return current datetime"""
    if is_timezone:
        return datetime.datetime.now(timezone)
    return datetime.datetime.now()


def datetime_delta(delta: int) -> datetime.datetime:
    """return datetime delta"""
    return current_datetime() + datetime.timedelta(days=delta)  # noqa: E501


def random_sleep(mu=1, sigma=0.4):
    """正态分布随机睡眠时间
    :param mu: 平均值
    :param sigma: 标准差
    """
    secs = random.normalvariate(mu, sigma)
    if secs < 0.1:
        secs = mu
    time.sleep(secs)


if __name__ == '__main__':
    a = datetime.datetime.now()
    a = datetime_to_isodate(a)
    print(a, type(a), a.isoformat())
